import Tool from './tool.js'
const urls = {
	postMail: "/fmail", // 发送邮件
	getSessionId: "/wechat/getSessionId",
	login: "/wechat/login",
	getOne: "/essay/one",
	getPool: "/pool"
}

const install = (Vue, vm) => {
	let postMail = (data) => {
		const userId = Tool.getStorage('openId', 'USER')
		if (userId) {
			data.userId = userId;
		}
		return vm.$u.post(urls.postMail, data)
	}

	let getSessionId = (params) => vm.$u.get(urls.getSessionId, params)

	let login = (userId) => {
		const userInfo = Tool.getStorage("userInfo")
		userId = userId || Tool.getStorage('openId', 'USER')
		return vm.$u.post(urls.login, {
			userId,
			userName: userInfo.nickName,
			city: userInfo.city,
			avatar: userInfo.avatarUrl,
			sex: userInfo.gender
		})
	}

	let getPool = (arr) => {
		let query = ""
		if (arr.length > 1) {
			query = arr.join(",")
		} else if (arr.length === 1) {
			query = arr[0]
		}
		if (!query) {
			return
		}
		return vm.$u.get(urls.getPool, {
			code: query
		})
	}

	let getOne = () => vm.$u.get(urls.getOne)

	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.api = {
		postMail,
		getSessionId,
		getOne,
		login,
		getPool
	};
}

export default {
	install
}
